package com.icedata.sika.util

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.util.concurrent.locks.ReadWriteLock

suspend fun ReadWriteLock.readAsync(block: suspend () -> Unit) {
    with(readLock()) {
        lock()
        GlobalScope.launch { block() }
        try {
            unlock()
        } catch (e: Exception) {
            Logger.logError(e)
        }
    }
}

suspend fun ReadWriteLock.writeAsync(block: suspend () -> Unit) {
    with(writeLock()) {
        lock()
        GlobalScope.launch { block() }
        try {
            unlock()
        } catch (e: Exception) {
            Logger.logError(e)
        }
    }
}